package com.csgsystems.im.survey;

import java.io.Serializable;

import org.apache.commons.lang.Validate;

public class Question implements Serializable, Comparable<Question> {
	/**
	 * 
	 */
	private static final long serialVersionUID = -5805937723679024760L;

	private QuestionKey questionKey = null;
	private Integer questionTypeId = null;
	private String questionText = null;
	private double weight = 0.0;

	public Question() {

	}

	public Question(QuestionKey questionKey, Integer questionTypeId,
			String questionText, double weight) {
		Validate.notNull(questionKey, "Question key cannot be null");
		Validate.notNull(questionTypeId, "Question type id cannot be null");
		Validate.notNull(questionText, "Question text cannot be null");
		this.questionKey = questionKey;
		this.questionTypeId = questionTypeId;
		this.questionText = questionText;
		this.weight = weight;

	}

	public Long getSurveyId() {
		if (questionKey == null) {
			return null;

		} else {
			return questionKey.getSurveyId();

		}

	}

	public Integer getQuestionId() {
		if (questionKey == null) {
			return null;

		} else {
			return questionKey.getQuestionId();

		}

	}

	public Integer getLanguageId() {
		if (questionKey == null) {
			return null;

		} else {
			return questionKey.getLanguageId();

		}

	}

	public Integer getQuestionTypeId() {
		return questionTypeId;
	}

	public void setQuestionType(Integer questionTypeId) {
		this.questionTypeId = questionTypeId;
	}

	public String getQuestionText() {
		return questionText;
	}

	public void setQuestionText(String questionText) {
		this.questionText = questionText;
	}

	public double getWeight() {
		return weight;
	}

	public void setWeight(double weight) {
		this.weight = weight;
	}

	@Override
	public int compareTo(Question thatQuestion) {
		int result = 0;
		if (thatQuestion == null) {
			result = 1;

		} else if (this == thatQuestion) {
			result = 0;

		} else {
			result = questionKey.compareTo(thatQuestion.questionKey);

		}
		return result;

	}

	public static class QuestionKey implements Serializable,
			Comparable<QuestionKey> {

		/**
		 * 
		 */
		private static final long serialVersionUID = -2505410859259365462L;

		private Long surveyId = null;
		private Integer questionId = null;
		private Integer languageId = null;

		public QuestionKey() {

		}

		public QuestionKey(Long surveyId, Integer questionId, Integer languageId) {
			Validate.notNull(surveyId, "Survey cannot be null");
			Validate.notNull(questionId, "Question id cannot be null");
			Validate.notNull(languageId, "Language id cannot be null");
			
			this.surveyId = surveyId;
			this.questionId = questionId;
			this.languageId = languageId;

		}

		public Long getSurveyId() {
			return surveyId;
		}

		public Integer getQuestionId() {
			return questionId;
		}

		public Integer getLanguageId() {
			return languageId;
		}

		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result
					+ ((languageId == null) ? 0 : languageId.hashCode());
			result = prime * result
					+ ((questionId == null) ? 0 : questionId.hashCode());
			result = prime * result
					+ ((surveyId == null) ? 0 : surveyId.hashCode());
			return result;
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			QuestionKey other = (QuestionKey) obj;
			if (languageId == null) {
				if (other.languageId != null)
					return false;
			} else if (!languageId.equals(other.languageId))
				return false;
			if (questionId == null) {
				if (other.questionId != null)
					return false;
			} else if (!questionId.equals(other.questionId))
				return false;
			if (surveyId == null) {
				if (other.surveyId != null)
					return false;
			} else if (!surveyId.equals(other.surveyId))
				return false;
			return true;
		}

		@Override
		public int compareTo(QuestionKey thatKey) {
			int result = 0;
			if (thatKey == null) {
				result = 1;

			} else if (this == thatKey) {
				result = 0;

			} else {
				long thisSurveyId = surveyId;
				long thatSurveyId = thatKey.surveyId;
				
				if (thisSurveyId < thatSurveyId) {
					result = -1;
					
				} else if (thisSurveyId > thatSurveyId) {
					result = 1;
					
				} else {
					if ((result = questionId.compareTo(thatKey.questionId)) != 0) {
						
					} else if ((result = languageId.compareTo(thatKey.languageId)) != 0) {
						
					}
					
				}
				
			}
			return result;

		}

		@Override
		public String toString() {
			StringBuilder sb = new StringBuilder("{");
			if (surveyId == null) {
				sb.append("null");

			} else {
				sb.append(surveyId);

			}
			sb.append(", ");
			sb.append(questionId);
			sb.append(", ");
			sb.append(languageId);
			sb.append("}");
			return sb.toString();

		}

	}
}
